//Project: MOTO_CSJ_61FC3F.prj
// Device: FT61FC3X
// Memory: Flash 3KX14b, EEPROM 128X8b, SRAM 256X8b
// Author: 
//Company: 
//Version:
//   Date: 
//===========================================================
//===========================================================
#include	"SYSCFG.h"
//===========================================================
//Variable definition
//===========================================================
#define  uchar     unsigned char 
#define  uint      unsigned int
#define  ulong     unsigned long

#define unchar     	unsigned char 
#define unint       unsigned int
#define unlong 		unsigned long

#define  uint8_t     unsigned char 
#define  uint16_t      unsigned int
#define  uint32_t     unsigned long

// pin1 -VDD
// pin2 - ISP_DATA --->> NC
// pin3 - ISP_CLK -->> NC
// pin4 - PB3 ---->>> 远光PWM
// pin5 - PB2 ---->>> 电机PWM
// pin6 - PA7 ---->>> ADC
// pin7 - PA4 ---->>> 远光输入检测 
// pin8 - PA2 ---->>> NC
// pin9 - PA0 ---->>> 近光PWM1
// pin10- GND


// #define FAR_DRIVER_PWM()  PA0=0;PB2=PB3=1;
// #define NEAR_DRIVER_PWM() PB2=PB3=0;PA0=1;
// SU01-A2  简要功能说明
// 近光状态：
// 近光2：亮
// 磁吸附电机：正向通电

// 远光状态：
// 近光2：灭
// 远光1、远光2：亮
// 磁吸附电机：反向通电

//===========================================================
//Function name：interrupt ISR
//parameters：无
//returned value：无
//===========================================================
#define TIMER_MAIN_CNT			5    // 5ms     5000/100 = 50
volatile uint mTime_MainLoopCnt;
volatile uint mMotor_DelayCloseCnt;
volatile uint mMotor_slowDownCnt;
#define MOTOR_SLOW_DOWN_TIME 200
#define MOTOR_DUTY_MAX 1000
#define MOTOR_DUTY_MIN 300

volatile uint mMotor_Duty;
volatile uint mMotor_Duty_OLD=0;

#define STATUE_DECT_GPIO 	PA4

#define  MOTOR_F()  \
	do{ \
		mMotor_Duty = MOTOR_DUTY_MAX;\
		mMotor_DelayCloseCnt=600;\
	}while(0);

#define  MOTOR_Z()  		mMotor_Duty=0;//PA4=1;PA2=0;

#define HIBYTE(v1)              ((uint8_t)((v1)>>8))                      //v1 is UINT16
#define LOBYTE(v1)              ((uint8_t)((v1)&0xFF))

void UserDriverDeal(void);

typedef struct
{
	uint16_t min_temp;
	uint16_t max_temp;
	uint16_t max_duty;
	uint16_t min_duty;
	uint16_t reset_temp;
}_Driver_duty_t;

#define DRIVER_TEMP_MIN 75
#define DRIVER_TEMP_MAX 120

_Driver_duty_t LedCalc[3]=
{
	{DRIVER_TEMP_MIN,DRIVER_TEMP_MAX,950,659,100}, 	// JG 4.86A->
	{DRIVER_TEMP_MIN,DRIVER_TEMP_MAX,860,389,100},   // JG_1 4A->
	{DRIVER_TEMP_MIN,DRIVER_TEMP_MAX,950,490,100}, 	//  YG
};


uint  TempValAdc;    // 温度Adc值
uint  TempVal;       // 具体温度
uint  JG_duty;	 // 近光-占空比
uint  YG_duty;	 // 远光 - 占空比

uint  JG_duty_OLD=0;	 // 近光-占空比
uint  YG_duty_OLD=0;	 // 远光 - 占空比

void UserTemperDeal(void);


#define  LED_STATUS_NEAR		0  // 	近光灯
#define  LED_STATUS_FAR			1  //   远光灯

uchar mDEV_WorkMode;
uchar mDEV_LedStatus;
uchar mDEV_isPowerOnFlag;
uint8_t ucFlagFastChange;
void interrupt ISR(void)
{
	//定时器0的中断处理**********************
	if(T0IE && T0IF)   {	//8.192ms翻转一次≈60Hz  //  note by yww : 1.024ms翻转一次
		//TMR0 = 100;		//注意:对TMR0重新赋值TMR0在两个周期内不变化
		T0IF = 0;
		mTime_MainLoopCnt++;
	} 
}
//===========================================================
//Function name：main
//parameters：无
//returned value：无
//===========================================================

void SystemTime0Init(void)
{
	OPTION = 0B00000101;	//Bit5 T0CS Timer0时钟源选择 
							//1-外部引脚电平变化T0CKI 0-内部时钟(FOSC/2)
							//Bit4 T0CKI引脚触发方式 1-下降沿 0-上升沿
							//Bit3 PSA 预分频器分配位 0-Timer0 1-WDT 
							//Bit2:0 PS2 8个预分频比 111 - 1:256 
	T0CON0 = 0x01;		//T0时钟源选择HIRC
	T0IF = 0;		    //清空T0软件中断	
	T0IE = 1;			//开定时器/计数器0中断                                               
	T0ON=1;
}

/*------------------------------------------------- 
 *	函数名称：DelayUs
 *	功能：   短延时函数 --16M-2T--大概快1%左右.
 *	输入参数：Time 延时时间长度 延时时长Time Us
 *	返回参数：无 
 -------------------------------------------------*/
 void DelayUs(unsigned char Time)
 {
	 unsigned char a;
	 for(a=0;a<Time;a++)
	 {
		 NOP();
	 }
 }

void WDT_INITIAL (void) 
{  
	CLRWDT();  				//清看门狗
	PSA=0;     				//时钟分频分给WDT
	WDTCON = 0B00001010;	//WDTPS=0101=1:1024,PS=000=1:1
					 		//定时时间=(1024*1)/32000=32ms
}
#define CONFIG_ADC_GPIO			ANSEL0=0B00100000;
#define TEMP_ADC_CH			    5
#define   ADC_Temper_CH  		5
void SystemAdcInit(void)
{
	CONFIG_ADC_GPIO;
	ANSEL0 = 1<<TEMP_ADC_CH;	//模拟口设置，AN0为模拟管脚

	ADCON1 = 0B11100101;	//右对齐，转换时钟Fosc/64，负参考电压GND，正参考电压内部电压(2V)
	//BIT7: 
		//1 = 右对齐。装入转换结果时，ADRESH的高4位被设置为0; 
		//0 = 左对齐。装入转换结果时，ADRESL的低4位被设置为0。
  
	//BIT6~BIT4:ADC转换时钟选择位
		//000 = FOSC/2
		//001 = FOSC/8
		//010 = FOSC/32
		//011 = FRC（由专用RC振荡器提供时钟）
		//100 = FOSC/4
		//101 = FOSC/16
		//110 = FOSC/64
		//111 = FRC（由专用RC振荡器提供时钟）
	
	//BIT3~BIT2:ADC负参考电压配置位（使用PB6连接外部参考电压或外部电容）
		//00 = Int Vref（内部参考电压）
		//01 = GND
		//10 = Int Vref + Ext Cap（内部参考电压 + 外部电容）
		//11 = Ext Vref（外部参考电压）

	//BIT1~BIT0 ADC正参考电压配置位（使用PB5连接外部参考电压或外部电容）
		//00 = Int Vref（内部参考电压）
		//01 = VDD
		//10 = Int Vref + Ext Cap（内部参考电压 + 外部电容）
		//11 = Ext Vref（外部参考电压）
							 
	ADCON0 = 0B00010000;	//选择AD转换通道0，使能ADC
	//BIT6~BIT3:ADC模拟通道选择位
		//0000 = AN0
		//0001 = AN1
		//0010 = AN2
		//0011 = AN3
		//0100 = AN4
		//0101 = AN5
		//0110 = AN6
		//0111 = 1/4 VDD
		//其余保留

	//BIT3: 保留位

	//BIT2: ADC触发信号类型选择
		//该位决定启动ADC的触发条件
		//0 = 当软件设定GO/DONE位，启动AD转换
		//1 = 需要外部触发信号触发才可启动AD转换，触发事件置位GO/DONE位。
		//外部触发信号条件由寄存器ETGSEL<2:0>和ETGTYP<1:0>决定。
  
	//BIT1:
		//0 = A/D转换完成/未进行。
		//1 = A/D转换正在进行或硬件触发延时正在计数。

	//BIT0:使能ADC  
		//0 = ADC被禁止且不消耗工作电流
		//1 = ADC被使能
			  
	//ADCON2 寄存器名字识别不了						
	ADINTREF0=1;
	DelayUs(200);//必须要延时450us  DELAY	450US	
	DelayUs(250);									
	//ADCON2 = 0B01000000;	//选择内部正参考电压2V，无外部触发源
	//BIT7~BIT6:ADC内部参考电压配置位
		//00 = 0.5V
		//01 = 2V
		//10 = 3V
		//11 = float（悬空）
		
	//BIT5~BIT4:外部触发信号类型选择
		//当ADEX置1，该位决定响应外部触发的类型
		//00 = PWM 或 ADC_ETR脚的下降沿
		//01 = PWM 或 ADC_ETR脚的上升沿
		//10 = 一个PWM周期的中点
		//11 = 一个PWM周期的终点
		
	//BIT3:ADC外部触发延时计数器阈值 第8位
		
	//BIT2~BIT0:外部触发源选择
		//当ADEX为1，该位选择外部触发ADC的来源
		//选择PWM源时需要配置TIMER为PWM输出模式并使能输出。
		//000 = PWM0
		//001 = PWM1
		//010 = PWM2
		//011 = PWM3
		//100 = PWM4
		//101 = PWM5
		//110 = PWM6
		//111 = ADC_ETR

	ADCON3 = 0B00000000; 
	//BIT7:ADC比较结果响应故障刹车使能
		//0 = 禁止
		//1 = ADC触发故障刹车功能使能
		
	//BIT6:ADC比较器输出极性选择位
		//0 = 若ADC结果的高八位大于或等于ADCMPH[7:0]，ADCMPO为1
		//1 = 若ADC结果的高八位小于ADCMPH[7:0]，ADCMPO为1

	//BIT5:ADC结果比较使能位
		//0 = ADC结果比较功能关闭
		//1 = ADC结果比较功能打开
		
	//BIT4:ADC比较结果输出位
		//该位输出ADCMPOP设定的比较输出结果。每次AD转换结束都会更新输出
		
	//BIT3:前沿消隐周期结束后，ADC触发使能
		//1 = 触发ADC转换
		//0 = 不触发ADC转换
		
	//BIT2:保留位
	//BIT1~BIT0 外部LVD管脚输入选择，只有当LVDM为1时才有效
		//00 = ELVD0
		//01 = ELVD1
		//10 = ELVD2
		//11 = ELVD3

	ADDLY  = 0B00000000;	//外部触发廷时，没用到
	//ADC外部触发启动延时计数器阈值低位
	//该8位寄存器与ADCON2.7组成9位计数器，用于在外部触发启动ADC之前加入一段延迟。延迟计数器结束再开始ADC转换
	//外部延迟时间 = (ADDLY+6)/FADC

	ADCMPH = 0B00000000;	//ADC比较阈值,仅8位，用于ADC结果高8位比较。

	
	ADON=1; //全能ADC
}			


// PB3 - P1D0 --- 远光
// PB2 - P1C0  --- 电磁阀
// PA0 - P1A2N --- 近光
// PWM 无后分频器
#define  CONFIG_PWM_GPIO   TRISB3=0;TRISA0=0;
void SystemPwmInit(void)
{
	CONFIG_PWM_GPIO;    

	T2CON0=0B00011000;   //;BIT6:3]=TOUTPS	后分频比	0000=1:1~1111=1:16
	            //;BIT2=TMR2ON		1=TIMER2打开							0=关闭
	            //;BIT[1:0]=T2CKPS	00=1:1	01=1:4	1x=1:16
    T2CON1=0B00000100;    // - 2xHIRC = 32Mhz 
                               
	//T2CKSRC0=0x02;
	PR2H=HIBYTE(999); 
    PR2L=LOBYTE(999); //181; //2.74Khz

	P1OE2= 0B00100000; // bit2-使能输出P1C1OE  bit5-P1D0OE
	P1OE =  0B10100000; // bit7-使能输出P1C0OE  bit5-P1A2N
	P1POL = 0B10100000; // bit7 - p1cop
	P1POL2= 0B00000000; // 正常输出
    
	TMR2H=0;
    TMR2L=0;  
    
	P1DDTH=0;  // PB3
   	P1DDTL=0;
	
	P1CDTH=0; // PB2
	P1CDTL=0;
	
	P1ADTH=0; // PA0
	P1ADTL=0;
	
    T2CON0=T2CON0|0x04;
}

/*-------------------------------------------------
 *  函数名: GET_ADC_DATA
 *	功能：  读取通道ADC值
 *  输入：  adcChannel 通道序号
 *  输出：  INT类型AD值(单次采样无滤波)
 --------------------------------------------------*/
 unint GET_ADC_DATA (unchar adcChannel) 
 { 
	 ADCON0 &= 0B00001111;        				   
	 ADCON0 |= adcChannel<<3; 				//重新加载通道值
	 DelayUs(40);                        	//廷时等待电压稳定 Tst >10us
	 ADCON0 = ADCON0|0x02;             					//启动ADC 
	 NOP();
	 NOP();
	 //PC0 =1;
	 while(ADCON0&0x02);    							//等待ADC转换完成
	 //PC0 =0;
 
	 return  (unint)(ADRESH<<8|ADRESL);      //整合12位AD值
 } 

void UserInit(void)
{
	if(STATUE_DECT_GPIO == 0){
		JG_duty =LedCalc[0].max_duty;
		YG_duty= 0;
		mDEV_LedStatus = LED_STATUS_NEAR;
		MOTOR_Z();
	}else{
		mDEV_LedStatus = LED_STATUS_FAR;
		JG_duty = LedCalc[1].max_duty;
		YG_duty= LedCalc[2].max_duty;
		// MOTOR_F();
	}
}

// pin1 -VDD
// pin2 - ISP_DATA
// pin3 - ISP_CLK
// pin4 - PB3 ---->>> 远光PWM3
// pin5 - PB2 ---->>> 远光检测---高电平有效
// pin6 - PA7 ---->>> ADC
// pin7 - PA4 ---->>> 反转输入 
// pin8 - PA2 ---->>> 正转输入
// pin9 - PA0 ---->>> 近光PWM1
// pin10- GND
#if 0
#define TEMPERA_VAL_MAX  125
#define TEMPERA_VAL_MIN  50
#define SEARCH_TABLE_SIZE  (TEMPERA_VAL_MAX-TEMPERA_VAL_MIN+1)
const uint NTC_Table[SEARCH_TABLE_SIZE]=
{
	0x320,0x31A,0x313,0x30C,0x305,0x2FE,0x2F7,0x2F0,0x2E8,0x2E1,
	0x2D9,0x2D2,0x2CA,0x2C2,0x2BB,0x2B3,0x2AB,0x2A3,0x29B,0x293,
	0x28B,0x283,0x27B,0x273,0x26A,0x262,0x25A,0x252,0x24A,0x242,
	0x239,0x231,0x229,0x221,0x219,0x211,0x208,0x200,0x1F8,0x1F0,
	0x1E8,0x1E0,0x1D8,0x1D1,0x1C9,0x1C1,0x1BA,0x1B2,0x1AB,0x1A3,
	0x19C,0x195,0x18E,0x187,0x180,0x179,0x173,0x16C,0x166,0x15F,
	0x159,0x153,0x14C,0x146,0x141,0x13B,0x135,0x12F,0x12A,0x124,
	0x11F,0x11A,0x114,0x10F,0x10A,0x105,
};

 uint search_handle(uint adc_value)
 {
	 uint b_min;
	 uint b_max;
	 uint b_mid;
	 uint temp16;
 
	 if(adc_value<=NTC_Table[SEARCH_TABLE_SIZE-1]) {
		 temp16=TEMPERA_VAL_MAX;
	 } else if(adc_value>=NTC_Table[0]) {
		 temp16=TEMPERA_VAL_MIN;
	 } else {
		 b_min = 0;
		 b_max = SEARCH_TABLE_SIZE;
		 while ((b_max-b_min)!=1)  {                           // 2分法查表。
			 b_mid = (b_min + b_max) >> 1;
			 if (adc_value < NTC_Table[b_mid]) {
				 b_min=b_mid;
			 } else if (adc_value > NTC_Table[b_mid]) {
				 b_max=b_mid;
			 } else {
				 temp16 = b_mid;
				 b_max=b_min+1; // 给退出循环增加一个条件
				 break; // 直接跳出循环
			 }
		 }
		 temp16=b_mid+TEMPERA_VAL_MIN;
	 }
 
	 return temp16;
 }
#else 

//温度范围是: 0°C to 135°C
//NTC阻值为:10000,分压电阻为:1000,NTC接GND
#define NTC_TABLE_SIZE 136
#define NTC_TABLE_TEMP_MIN 0
const unsigned int NTC_adc_table[NTC_TABLE_SIZE]={
0x0f88,0x0f82,0x0f7c,0x0f75,0x0f6e,0x0f66,0x0f5f,0x0f57,0x0f4f,0x0f46,
0x0f3d,0x0f34,0x0f2a,0x0f20,0x0f16,0x0f0b,0x0f00,0x0ef5,0x0ee9,0x0edd,
0x0ed0,0x0ec3,0x0eb5,0x0ea7,0x0e99,0x0e8a,0x0e7b,0x0e6b,0x0e5b,0x0e4a,
0x0e39,0x0e28,0x0e16,0x0e03,0x0df0,0x0ddd,0x0dc9,0x0db5,0x0da0,0x0d8a,
0x0d75,0x0d5e,0x0d48,0x0d31,0x0d19,0x0d01,0x0ce8,0x0ccf,0x0cb6,0x0c9c,
0x0c82,0x0c67,0x0c4c,0x0c31,0x0c15,0x0bf9,0x0bdd,0x0bc0,0x0ba3,0x0b86,
0x0b68,0x0b4a,0x0b2c,0x0b0d,0x0aef,0x0ad0,0x0ab1,0x0a91,0x0a72,0x0a52,
0x0a33,0x0a13,0x09f3,0x09d3,0x09b3,0x0993,0x0973,0x0953,0x0933,0x0913,
0x08f3,0x08d3,0x08b3,0x0893,0x0874,0x0854,0x0835,0x0815,0x07f6,0x07d7,
0x07b9,0x079a,0x077c,0x075e,0x0740,0x0722,0x0705,0x06e8,0x06cb,0x06ae,
0x0692,0x0676,0x065b,0x063f,0x0624,0x0609,0x05ef,0x05d5,0x05bb,0x05a2,
0x0589,0x0570,0x0557,0x053f,0x0528,0x0510,0x04f9,0x04e2,0x04cc,0x04b6,
0x04a0,0x048b,0x0476,0x0461,0x044d,0x0439,0x0425,0x0412,0x03ff,0x03ec,
0x03da,0x03c8,0x03b6,0x03a5,0x0394,0x0383,
};
uint16_t binary_search_adc_table(uint16_t adc_value) {
    int left = 0;
    int right = NTC_TABLE_SIZE - 1;
    int mid;

    while (left <= right) {
        mid = left + (right - left) / 2;

        if (NTC_adc_table[mid] == adc_value) {
            return mid + NTC_TABLE_TEMP_MIN;
        } else if (NTC_adc_table[mid] > adc_value) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }

    // 如果没有找到完全匹配的值，返回最接近的较小值的索引
    if (left > 0) {
        return (left - 1)+NTC_TABLE_TEMP_MIN;
    } else {
        return NTC_TABLE_TEMP_MIN;
    }
}
#endif


uint Cur_TempVal;
void UserTemperDeal(void)
{
    static uint up_count = 0;
    static uint down_count = 0;
    const uint DEBOUNCE_TIME_MS = 1000; // 防抖时间1000ms
    const uint CALL_INTERVAL_MS = 5; // 调用间隔5ms
    const uint DEBOUNCE_COUNT = DEBOUNCE_TIME_MS / CALL_INTERVAL_MS; // 防抖计数次数
    

    TempValAdc = GET_ADC_DATA(ADC_Temper_CH);
	#if 1
	// TempValAdc = TempValAdc >> 2;
    if((TempValAdc > 4000) || (TempValAdc < 20)){
        Cur_TempVal = 135;
    } else 
	{
        Cur_TempVal = binary_search_adc_table(TempValAdc);
    }
	Cur_TempVal +=7; // 补偿4摄氏度
	#else
	TempValAdc = TempValAdc >> 2;
	if((TempValAdc > 1000) || (TempValAdc < 20)){
        Cur_TempVal = TEMPERA_VAL_MAX;
    } else {
        Cur_TempVal = search_handle(TempValAdc);
    }
	//Cur_TempVal = search_handle(TempValAdc);
	// Cur_TempVal +=4; // 补偿4摄氏度
	#endif

	

    // if (TempVal > Cur_TempVal) {
    //     // 温度下降
    //     up_count = 0;
    //     if (++down_count >= DEBOUNCE_COUNT) {
    //         down_count = 0;
    //         if (TempVal > 0) {
    //             TempVal--;
    //         }
    //     }
    // } else if (TempVal < Cur_TempVal) {
    //     // 温度上升
    //     down_count = 0;
    //     if (++up_count >= DEBOUNCE_COUNT) {
    //         up_count = 0;
	// 		TempVal++;

			
    //     }
    // } else {
    //     // 温度不变
    //     up_count = 0;
    //     down_count = 0;
    // }


	// if(TempVal < DRIVER_TEMP_MIN){
	// 	TempVal = DRIVER_TEMP_MIN;
	// }  

	// if(TempVal > DRIVER_TEMP_MAX){
	// 	TempVal = DRIVER_TEMP_MAX;
	// }  
	TempVal= Cur_TempVal;
}


// CurTimeCount --- 当前时间计数器---使用倒计时方式
// RangeTime    --- 时间范围
// duty_min  --- 时间为0时的占空比
// duty_max  --- 时间为max时的占空比
// 用于计算占空比缓降处理
uint16_t CalcDutySlowDownDeal(uint16_t CurTimeCount, uint16_t RangeTime, uint16_t duty_min , uint16_t duty_max)
{
	unsigned long TempU32;
	TempU32 = duty_max - duty_min;
	TempU32 *= CurTimeCount; 
	TempU32 /= RangeTime;
	TempU32 += duty_min;

	if(duty_min > duty_max){
		return duty_max;
	}

	return (uint16_t)TempU32;
}


void main(void)
{
//===========================================================
//除TSSOP/SOP20封装外，PA3、PA5必须配置成输出低,程序中请勿修改
	TRISA3 = 0;			 
	TRISA5 = 0;			
	PA3 = 0;			 
	PA5 = 0;
	ANSEL0=0B00000000;

// pin4 - PB3 ---->>> 远光PWM
// pin5 - PB2 ---->>> 电机PWM
// pin6 - PA7 ---->>> ADC
// pin7 - PA4 ---->>> 远光输入检测 
// pin8 - PA2 ---->>> NC
// pin9 - PA0 ---->>> 近光PWM1

	// CONFIG OUTPUT
	TRISA0=0;
	TRISA2=0;
	TRISA4=0; 
	TRISB3=0;
	TRISB2=0;

	// CONFIG INPUT
	TRISA7=1;
	TRISA4=1;

   SystemTime0Init();
   SystemPwmInit(); 
   SystemAdcInit();
   UserInit();
   WDT_INITIAL(); 
   GIE  = 1;				//开总中断  
   PEIE = 1;
   SWDTEN=1;

   while (1)
   {
	   CLRWDT();
	   
	   UserDriverDeal();
	   UserTemperDeal();
	   while (mTime_MainLoopCnt < TIMER_MAIN_CNT) // 5.12ms
	   {
	   }
	   mTime_MainLoopCnt = 0;
   }
}

#define CHANGE_UP_TIME_COUNT 100  // 1s
#define CHANGE_FLASH_TIMECOUNT 6

void UserLedStatusDeal(void)
{
	static uchar ChangeLevelCnt = 0; // 用于切换统计
	if(mDEV_LedStatus == LED_STATUS_NEAR) // 近光...
	{
		if(STATUE_DECT_GPIO == 1) 
		{
			if(++ChangeLevelCnt>=CHANGE_FLASH_TIMECOUNT)  // 30ms
			{
				ChangeLevelCnt = 0;
				mDEV_LedStatus = LED_STATUS_FAR;
				JG_duty= LedCalc[1].max_duty;
				YG_duty= LedCalc[2].max_duty;
				MOTOR_F();
			}
		} else {
			ChangeLevelCnt=0;
		}
	}
	else //if(mDEV_LedStatus == LED_STATUS_FAR)
	{

		if(mMotor_DelayCloseCnt){
			mMotor_DelayCloseCnt--;
			mMotor_Duty  = MOTOR_DUTY_MAX;
			if(mMotor_DelayCloseCnt == 0){
				mMotor_slowDownCnt = MOTOR_SLOW_DOWN_TIME;
			}
		}else {
			if(mMotor_slowDownCnt){
				mMotor_slowDownCnt--;
				mMotor_Duty = CalcDutySlowDownDeal(mMotor_slowDownCnt, MOTOR_SLOW_DOWN_TIME, MOTOR_DUTY_MIN, MOTOR_DUTY_MAX);
			}
			else {
				mMotor_Duty  = MOTOR_DUTY_MIN;
			}
		}

		if(STATUE_DECT_GPIO == 0)
		{
			if(++ChangeLevelCnt>=CHANGE_FLASH_TIMECOUNT)
			{
				ChangeLevelCnt = 0;
				mDEV_LedStatus = LED_STATUS_NEAR;
				JG_duty= LedCalc[0].max_duty;
				YG_duty= 0x00;
				MOTOR_Z();
			}
		}
		else
		{
			ChangeLevelCnt=0;
		}
	}
}


uint16_t DrverCalcDuty(uint16_t tempVal, uint8_t index)
{
	uint32_t tempU32;
	uint16_t tempDuty;
	if(tempVal <= LedCalc[index].min_temp)
	{
		tempDuty = LedCalc[index].max_duty;
	} else	if(tempVal >= LedCalc[index].max_temp) {
		tempDuty = LedCalc[index].min_duty;
	} else {
		tempU32 = (LedCalc[index].max_temp -tempVal)*(LedCalc[index].max_duty-LedCalc[index].min_duty);
		tempU32+= (LedCalc[index].max_temp-LedCalc[index].min_temp)*LedCalc[index].min_duty;
		tempU32 = tempU32/(LedCalc[index].max_temp-LedCalc[index].min_temp);
			
		tempDuty = (uint16_t)tempU32;
	}

	return tempDuty;
}

void UserDriverDeal(void)
{
	// UserLedStatusDeal();

	uint tempIndex;
	uint PWM1_duty;
	uint PWM2_duty;
	static uint  slowDownCnt = 0;
	static uint  slowDownCnt2 = 0;

	UserLedStatusDeal();
	

	if(mDEV_LedStatus == LED_STATUS_NEAR){
		PWM1_duty = DrverCalcDuty(TempVal,0);
		PWM2_duty  = 0;
	}else {
		PWM1_duty = DrverCalcDuty(TempVal,1);
		PWM2_duty  = DrverCalcDuty(TempVal,2);
	}

	//ucFlagFastChange = 1;
	if(ucFlagFastChange == 0) {
		if(slowDownCnt){
			slowDownCnt--;
		} else {
			slowDownCnt = 40;
		}

		if(slowDownCnt2){
			slowDownCnt2--;
		} else {
			slowDownCnt2 = 40;
		}

		if(JG_duty > PWM1_duty)  {
			if(slowDownCnt == 0) {
				JG_duty--;
			}
		} else if(JG_duty < PWM1_duty)  {
			if(slowDownCnt == 0) {
				JG_duty++;
			}
		}

		if(YG_duty > PWM2_duty)  {
			if(slowDownCnt2 == 0) {
				YG_duty--;
			}
		} else if(YG_duty < PWM2_duty)  {
			if(slowDownCnt2 == 0) {
				YG_duty++;
			}
		}
	}
	else {
		ucFlagFastChange = 0;
		JG_duty = PWM1_duty;
		YG_duty = PWM2_duty;
	}

	if(JG_duty_OLD!=JG_duty){
		JG_duty_OLD = JG_duty;
		P1ADTH=HIBYTE(JG_duty);
		P1ADTL=LOBYTE(JG_duty);
	}

	if(YG_duty_OLD != YG_duty){
		YG_duty_OLD = YG_duty;
		P1DDTH=HIBYTE(YG_duty);
		P1DDTL=LOBYTE(YG_duty);
	}


	#if 1
	if(mMotor_Duty_OLD != mMotor_Duty){
		mMotor_Duty_OLD = mMotor_Duty;
		P1CDTH=HIBYTE(mMotor_Duty);
		P1CDTL=LOBYTE(mMotor_Duty);
		
	}
	#else 
	P1CDTH=HIBYTE(300);
	P1CDTL=LOBYTE(300);
	#endif

// PB3 - P1D0 --- 远光
// PB2 - P1C0  --- 电磁阀
// PA0 - P1A2N --- 近光	

}

//===========================================================
